package com.i2works.smartluolong.web.controller.suggestion;


import com.google.common.collect.Lists;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.entity.Tree;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.org.OrgDO;
import com.i2works.smartluolong.web.entity.org.OrgQuery;
import com.i2works.smartluolong.web.entity.suggestion.SuggestionDO;
import com.i2works.smartluolong.web.service.org.IOrgService;
import com.i2works.smartluolong.web.service.suggestion.ISuggestionService;
import com.i2works.smartluolong.web.utils.BaseModule;

import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 投诉及建议
 *
 * @author liu Yapeng
 * @date 2018/7/23 20:07
 */

@Controller
@RequestMapping("/suggestion")
public class SuggestionController extends BaseModule {
	
	private final ISuggestionService   suggestionService;
	private final IOrgService   orgService;

	  public SuggestionController(ISuggestionService   suggestionService,IOrgService    orgService) {
		  this.suggestionService=suggestionService;
		  this.orgService=orgService;
	  }


      /**
       * 申请
       */
      @GetMapping("/apply")
      public String apply(Model model) {
	        List<Tree> result = Lists.newArrayList();
        try {
            OrgQuery query = new OrgQuery();
            query.setShowStatus(0);
            query.setStatus(1);
            query.setOrgType(2);
            List<OrgDO> orgList = orgService.queryList(query);
            model.addAttribute("type", 0);
            model.addAttribute("orgList", orgList);
        } catch (Exception e) {
            logger.error("", e);
        }
          return "suggestion/apply";
      }
      
      /**
       * 申请
       */
      @GetMapping("/apply/{type}")
      public String apply(@PathVariable("type") Integer type,Model model) {
        try {
            OrgQuery query = new OrgQuery();
            query.setStatus(1);
            query.setLimit(500L);
            List<OrgDO> orgList = orgService.queryList(query);
            model.addAttribute("orgList", orgList);
            model.addAttribute("type", type);
        } catch (Exception e) {
            logger.error("", e);
        }
          return "suggestion/apply";
      }
	    
	    /**
	     * 提交修改投诉建议
	     */
	    @PostMapping("/save")
	    @ResponseBody
	    public Result  save(SuggestionDO  suggestion) {
	    	try {
	    		suggestion.setCreateName(getUserName());
	    		suggestion.setUpdateName(getUserName());
	    	    suggestion.setUserId(getUserId());
				suggestionService.saveOrUpdate(suggestion);
			} catch (ServiceException e) {
				e.printStackTrace();
				 return R.error();
			}
	        return R.success();
	    }
    
	    
	

  
}
